﻿using System;
using System.Collections.Generic;
using log4net;
using MyLib.IO;
using System.IO;
using System.IO.Compression;

namespace MyLib.Web {
	/// <summary>
	/// 
	/// </summary>
	public class WebApplicationRegisterFileTypesEventArgs : EventArgs {
		/// <summary>
		/// 
		/// </summary>
		public IDictionary<int, string> FileTypes { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fileTypes"></param>
		public WebApplicationRegisterFileTypesEventArgs(IDictionary<int, string> fileTypes) {
			FileTypes = fileTypes;
		}
	} // end of WebApplicationRegisterFileTypesEventArgs.

	/// <summary>
	/// 
	/// </summary>
	public class MyWebApplication : System.Web.HttpApplication {
		/// <summary>
		/// 内部日志
		/// </summary>
		private ILog m_log = LogManager.GetLogger("Global");

		/// <summary>
		/// 
		/// </summary>
		public event EventHandler<WebApplicationRegisterFileTypesEventArgs> RegisterFileTypes;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_Start(object sender, EventArgs e) {
			// Initialize log4net.
			log4net.Config.XmlConfigurator.Configure();

			// Initialize session factory.
			SessionFactory.InitializeDefaultInstance("MyLib.Connection");

			var fileTypes = new Dictionary<int, string>();
			OnRegisterFileTypes(fileTypes);
			FileSystemProvider.RegisterFileTypes(fileTypes);

			m_log.Info("Application has started.");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fileTypes"></param>
		protected virtual void OnRegisterFileTypes(IDictionary<int, string> fileTypes) {
			if (RegisterFileTypes != null)
				RegisterFileTypes(this, new WebApplicationRegisterFileTypesEventArgs(fileTypes));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_PostRequestHandlerExecute(object sender, EventArgs e) {
			string encodings = Request.Headers.Get("Accept-Encoding");
			string contentType = Response.ContentType;

			if (string.IsNullOrEmpty(encodings) || string.IsNullOrEmpty(contentType))
				return;

			if (!contentType.StartsWith("text/"))
				return;

			Stream s = Response.Filter;

			encodings = encodings.ToLower();
			if (encodings.Contains("gzip")) {
				Response.Filter = new GZipStream(s, CompressionMode.Compress);
				Response.AppendHeader("Content-Encoding", "gzip");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_End(object sender, EventArgs e) {
			m_log.Info("Application has shutdown.");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_Error(object sender, EventArgs e) {
			var msg = string.Empty;
			if (Request != null) {
				msg = string.Format("Unhandled exception when request url \"{0}\".", Request.RawUrl);
			} else {
				msg = "Unhandled exception.";
			}

			if (m_log.IsErrorEnabled) {
				m_log.Error(msg, Server.GetLastError());
			}
		}

	} // end of MyWebApplication.
}
